#include <stdio.h>
#include <string.h>
#include <malloc.h>
// #include "otherFun.h"
#include "pointer.h"

void getMemory(char **p, int num)
{
    *p = malloc(num * sizeof(char));
    // return p;
}

char *fun(char *p1, char *p2)
{
    int i = 0;
    i = strcmp(p1, p2);
    char *result;
    if (0 == i)
    {
        result = p1;
    }
    else
    {
        result = p2;
    }

    return result;
}

void funPrint()
{
    printf("Call fun print\n");
}

int main(int argc, char const *argv[])
{
    int *p;
    char *p2;
    long *p3;
    printf("int pointer size : %ld value = %p \n", sizeof(p), p);
    printf("char pointer size : %ld\n", sizeof(p2));
    printf("long pointer size : %ld\n", sizeof(p3));
    printf("void pointer size : %ld\n", sizeof(void *));

    int *pTest = NULL;
    printf("pTest pointer size : %ld value = %p\n", sizeof(pTest), &pTest);

    int *pTest2;
    pTest2 = NULL;
    printf("pTest2 pointer size : %ld  value = %p\n", sizeof(pTest2), pTest2);

    int i = 0;
    printf("i value = %d , address = %p\n", i, &i);

    int *pi = &i;
    *pi = 10;
    // memcpy(pi,10,4);
    // (*(int *)0x7fffffffddb4) = 0x100;
    printf("i value = %d , address = %p\n", i, pi);

    printf("二级指针用法\n");

    char **pSecond = NULL; /*二级指针*/
    char *pFirst = "123\0 32";
    pSecond = &pFirst;

    printf("一级指针 pFirst value = %s\n", pFirst);
    printf("二级指针 pSecond value = %s , **pSecond value = %c\n", *pSecond, **pSecond);

    printf("二级指针函数内赋值\n");
    char *pStrTest = NULL;
    getMemory(&pStrTest, 10);
    strcpy(pStrTest, "hello");

    printf("pStrTest = %s\n", pStrTest);
    free(pStrTest);
    free(NULL);
    // pStrTest = NULL;
    if (NULL == pStrTest)
    {
        printf("内存已释放\n");
    }
    else
    {
        printf("内存未释放\n");
    }

    printf("\n函数指针\n");

    char *(*pf)(char *p1, char *p2);
    pf = &fun;
    printf("函数指针变量调用结果：%s\n", (*pf)("aa", "bb"));
    printf("直接调用结果：%s\n", fun("aa", "cc"));
    void (*pfun)();
    // *(int *)&pfun = (int)funPrint;
    pfun = &funPrint;
    (*pfun)();

    printf("开始调用其他文件中的方法\n");

    // char *(*pof)(char *name);
    // pof = &otherFun;

    // (*pof)("zoulx");

    struct USER ts;
    ts.age = 99;
    printf("指针结构体数据：age = %d \n", ts.age);

    printf("ts.name & = %p , value = %s\n", ts.name, ts.name);
    char *tsName = "张三";
    ts.name = &tsName;
    strcpy(ts.name, "李四");
    printf("ts.name & = %p , value = %s\n", ts.name, ts.name);
    printf("------------------------------END-------------------------\n");
}

